<?php 

error_reporting(E_ERROR);
require_once 'config.php';
createModels();

function createModels()
{
	$tables = Database::getTables();
	$diagram = @fopen('newmodel/diagram.txt', 'w+');
	fclose($diagram);
	
	$created = 0;
	echo "<pre>";
	foreach($tables as $table)
	{
		$file = 'newmodel/'.$table.'.php';
		$adminModel = 'model/admin/'.$table.'.php';
		
		if(file_exists($file) && !file_exists($adminModel)) 
		{	
			@shell_exec("rm $file");
		}

		
		if(!file_exists($adminModel))
		{
			$aModelGetters = array();
			$aNefubObjectGetters = array();
			$addGetByNefubId = false;
			$addGetByNefubName = false;
			
			$structure = Database::getTableStructure($table);
			echo "\n";		
			foreach($structure as $field=>$type)
			{			
				if(substr($field, -3, 3) == '_id' && $field != 'nefub_id')
				{
					if(substr($field, -9, 6) == '_nefub')
					{
						$nefubId = true;
						$model = substr($field, 0, strlen($field) - 9);
					}
					else
					{
						$nefubId = false;
						$model = substr($field, 0, strlen($field) - 3);
						
					}
					$model = explode('_', $model);
					$newModel ='';
					foreach($model as $part)
					{
						$newModel .= ucfirst($part);
					}
					$classField = lcfirst($newModel);
					$newModel = preg_replace('%[0-9]{1,}%', '', $newModel, 1);
					$newModel = str_replace('RefTeam', 'Team', $newModel);
					$aNefubObjectGetters[] = array(
						'class' => $newModel,
						'dbfield' => $field,
						'classfield' => $classField,
						'hasNefubId' => $nefubId);						
				}
				elseif($field == 'nefub_id')
				{
					$addGetByNefubId = true;
				}
				elseif($field == 'nefub_name')
				{
					$addGetByNefubName = true;
				}
				
			}
			
			$php = createNefubObject($table, $aNefubObjectGetters, $addGetByNefubId, $addGetByNefubName);
			echo '<textarea cols="120" rows="30">'.$php.' </textarea>';
			if($f = @fopen($file, 'w+'))
			{
				fwrite($f, $php);
				fclose($f);
				echo "\n".$file." aangemaakt.";
				$created++;
			}
			else
			{
				echo "\nKon ".$file." niet aanmaken.";				
			}
			
		}
	}
	echo "\n\nNefubObjects aangemaakt: ".$created."\n";
	echo "\n<pre>\n";
	echo '<textarea cols="120" rows="30">';
	require_once 'newmodel/diagram.txt';
	echo ' </textarea>';
}


function createNefubObject($class, $aFields, $addGetByNefubId, $addGetByNefubName)
{
	
	$diagram = @fopen('newmodel/diagram.txt', 'a');
	fwrite($diagram,"[$class]->[NefubObject]\n");
	$php =
'<?php
class '.$class.' extends Model
{';

foreach($aFields as $aField)
{
	$model = $aField['class'];
	$classField = $aField['classfield'];	
	
	$php .=
	'
	/**
	*
	* @var '.$model.'
	*/
	private $'.$classField.';
';
	}
	foreach($aFields as $aField)
	{
		$model = $aField['class'];
		$classField = $aField['classfield'];
		$dbField = $aField['dbfield'];
		if($aField['hasNefubId'])
		{
			$php .= getNefubGetter($model, $classField, $dbField);
		}
		else
		{
			$php .= getNormalGetter($model, $classField, $dbField);
			
		}
		
		fwrite($diagram,"[$model]-<>[$class]\n");
	}
	
	fclose($diagram);
	if($addGetByNefubId)
	{
		$php .= getNefubIdGetter($model);
	}
	if($addGetByNefubName)
	{
		$php .= getNefubNameGetter($model);
	}
	$php .= getAllGetter($model, $classField, $dbField);

$php .= '
}';
return $php;
}
	
	

function getNefubGetter($model, $classField, $dbField)
{
	return '
	/**
	*
	* @return '.$model.'
	*/
	public function get'.$model.'()
	{
		if(!$this->'.$classField.')
		{
			$this->'.$classField.' = '.$model.'::getByNefubId($this->'.$dbField.');
		}
	
		return $this->'.$classField.';
	}
';
}

function getNormalGetter($model, $classField, $dbField)
{
	return '
	/**
	*
	* @return '.$model.'
	*/
	public function get'.$model.'()
	{
		if(!$this->'.$classField.')
		{
			$this->'.$classField.' = new '.$model.'($this->'.$dbField.');
		}	
		return $this->'.$classField.';
	}
';
}

function getNefubIdGetter($model)
{
	return '
	/**
	 * 
	 * @param int $nefubId
	 * @return '.$model.'
	 */
	public static function getByNefubId($nefubId)
	{
		return self::getSingleModel(get_class(), array(\'nefub_id\' => $nefubId));
	}
';	
}

function getNefubNameGetter($model)
{
	return '
	/**
	 * 
	 * @param int $nefubName
	 * @return '.$model.'
	 */
	public static function getByNefubName($nefubName)
	{
		return self::getSingleModel(get_class(), array(\'nefub_name\' => $nefubName));
	}
';	
}

function getAllGetter()
{
	return '
	/**
	 * 
	 * @param array $where
	 * @param string $orderBy
	 * @param string $dir
	 * @param int $limit
	 * @return array
	 */
	public static function getAll($where = array(), $orderBy = null, $dir = \'ASC\', $limit = 0)
	{
		return self::getAllModels(get_class(), $where, $orderBy, $dir, $limit);
	}';
}
